//
//  CollectTableViewController.m
//  VEW
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 Y团. All rights reserved.
//

#import "CollectTableViewController.h"
#import "DetailViewController.h"



@interface CollectTableViewController ()

@property (nonatomic,strong) NSMutableArray *titleArray;
@property (nonatomic,strong) NSMutableArray *myIDArray;
@property (nonatomic,strong) NSMutableArray *selectAllDB;

@end

@implementation CollectTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    DataBaseManager *db = [DataBaseManager shareInstance];
    [db openDB];
    NSArray *array = [[[DataBaseManager shareInstance]selectAllNews]mutableCopy];
    
    self.titleArray = [NSMutableArray array];
    self.myIDArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    for (HomeModel *model in array) {
        NSString *string = model.title;
        NSString *string1 = model.MyID;
        
        [self.titleArray addObject:string];
        [self.myIDArray addObject:string1];
        [self.array addObject:model];
    }

}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"收藏";
    

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    
//    HomeModel * home = self.selectAll[indexPath.row];
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGRect rect = [UIScreen mainScreen].bounds;
    
    return rect.size.width *0.12;
}

//编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [[DataBaseManager shareInstance] deleteNewsForRow:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailViewController * detailVC = [[DetailViewController alloc] init];
    
    HomeModel *home=[[HomeModel alloc]init];
    
    home = self.array[indexPath.row];

    detailVC.homeData = home;
    

    detailVC.hidesBottomBarWhenPushed=YES;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
